/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */

import path from 'path';

import { expect } from '@jest/globals';

import { runHvigorAsync } from '../../common/common';
import { commonJson5Operation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
};

/**
 * 1、创建默认工程
 * 2、修改entry的oh-package.json5的name为123
 * 3、构建hap
 * 3、修改entry的oh-package.json5的name为"entry", version为123
 * 4、构建hap
 * 5、查看是否有BUILD FAILED
 *
 * @param {Project} project
 * @param {TestTaskOption} taskConfig
 * @param {ICaseRecord} record
 * @return res
 */
const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();
  const entryRootPath = path.resolve(projectRoot, 'entry');

  // entry的oh-package修改
  await commonJson5Operation(path.resolve(entryRootPath, 'oh-package.json5'), async (json5: any) => {
    json5.name = 123;
  });

  // 构建hap
  const commands = '--mode module -p product=default assembleHap'.split(' ');
  try {
    const result1 = await runHvigorAsync(projectRoot, commands);
    record.addStep('第一次构建hap，检查BUILD FAILED', result1);
    expect(result1.stdout).toContain('BUILD FAILED');
  } catch (error) {}
};

TestCaseBuilder.builder()
  .caseName('DTS2024110504869')
  .description('验证修改oh-package的名字或版本号为非法类型的报错退出')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.DTS).build())
  .testFun(testfun)
  .load();
